package StringType;

import java.util.HashSet;

/**
 * @Author ChanZany
 * @Date 2021/5/18 10:07
 * @Version 1.0
 */
public class LongestSubstr {
    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("abcabcbb"));
    }

    public static int lengthOfLongestSubstring(String s) {
        int right=-1,ans =0;
        char[] chars = s.toCharArray();
        int n = chars.length;
        HashSet<Character> OCC = new HashSet<>();
        for(int i = 0; i< n; i++){
            if (i!=0){
                OCC.remove(chars[i-1]);
            }
            while (right+1<n && !OCC.contains(chars[right+1])){
                OCC.add(chars[right+1]);
                right++;
            }
            ans = Math.max(ans,right-i+1);
        }
        return ans;
    }
}
